#include "registerr.h"
#include "ui_registerr.h"

registerr::registerr(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::registerr)
{
    ui->setupUi(this);

    this->setFixedSize(this->size());
}

registerr::~registerr()
{
    delete ui;
}

void registerr::on_btnReset_clicked()
{
    ui->editAccount->clear();
    ui->editName->clear();
    ui->editPhone->clear();
    ui->editPswd->clear();

    ui->editRolePro->clear();
    ui->combRoleSex->setCurrentIndex(0);
    ui->editRoleName->clear();


}


void registerr::on_btnSubmit_clicked()
{
    QString account = ui->editAccount->text();
    QString name = ui->editName->text();
    QString phone = ui->editPhone->text();
    QString pswd = ui->editPswd->text();

    QString rolePro = ui->editRolePro->text();
    QString roleName = ui->editRoleName->text();
    QString roleSex = QString::asprintf("%d",ui->combRoleSex->currentIndex());

    if(account.isEmpty()||name.isEmpty()||phone.isEmpty()||pswd.isEmpty()||rolePro.isEmpty()||roleName.isEmpty()){
        QMessageBox::warning(0,"警告","请填写完整的信息!");
        return;
    }

    QSqlQuery query;
    query.exec("select * from tb_admin where account='"+account+"';");
    if(query.size() != 0){
        QMessageBox::warning(0,"提示","该账号已存在.");
        return;
    }

    query.exec("select * from tb_user where account='"+account+"';");
    if(query.size() != 0){
        QMessageBox::warning(0,"提示","该账号已存在.");
        return;
    }

    QString attStr;
    attStr += roleName+"&";
    attStr += roleSex+"&";
    attStr += rolePro+"&";
    attStr += ui->labLv->text()+"&";
    attStr += ui->labGold->text()+"&";
    attStr += ui->labAtk->text()+"&";
    attStr += ui->labDef->text()+"&";
    attStr += ui->labMag->text();

    if(!query.exec("insert into tb_user(account,pswd,name,phone,attribute) values('"+account+"','"+pswd+"','"+name+"','"+phone+"','"+attStr+"');")){
        QMessageBox::warning(0,"警告","提交失败!");
        return;
    }

    QMessageBox::information(0,"提示","提交成功!");
    this->close();
    logui* l = new logui;
    l->show();
}

